package brushQuestions.seek;

import java.util.List;

/**
 * 折半查找，又名二分查找
 * 使用折半查找的条件：是列表元素有序排列
 *
 * @author: mengfanbing
 * @date: 2020/5/27 14:48
 */
public class BinarySearch {
    public static Integer testBinarySearch(List<Integer> list,Integer target,int start,int end) {
        if (target<list.get(start) || target>list.get(end) || start>end){
            return -1;
        }
        int i = (start + end)/2;
        Integer valueByIndex = list.get(i);
        if (target.equals(valueByIndex)){
            System.out.println("target下标=" + i);
            return i;
        }
        if (target > valueByIndex){
            start = i+1;
        }else {
            end = i-1;
        }
        Integer integer = BinarySearch.testBinarySearch(list, target,start,end);
        return integer;
    }
}
